﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PwdMgr.PwdMgrService.Sessions;

namespace PwdMgrUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (Root.Children != null && Root.Children.Count > 0)
                Root.Children.Clear();

            Login login = new Login();
            login.LoginCompleted += s =>
            {
                if (s.LoginSuccessfully)
                    AfterLoginSuccessfully(s.Session);
                else
                    MessageBox.Show("Login failed!");
            };
            SetMainContent(login);
        }

        private void AfterLoginSuccessfully(Session session)
        {
            this.tblk_Header.Text = "Welcome " + session.Username;
            if (App.Storage.ContainsKey("session"))
                App.Storage["session"] = session;
            else
                App.Storage.Add("session", session);

            SetMainContent(new Navigation());
        }

        private void SetMainContent(UIElement element)
        {
            Root.Children.Clear();
            Root.Children.Add(element);
        }
    }
}
